<?php
/*

  Copyright (c) 2008   by  maxim.oransky <maxim.oransky@gmail.com>,
  auhtor of  the-wheel-library  team

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class Mail {

	public static function send($from, $to, $subj, $text) {

	    if (!Env::isMailFunctionsEnabled()) return;

		$headers  = '';
		$headers .= "Content-Type: text/plain; charset=" . Settings::config('system')->get('site', 'encoding') . "\n";
		$headers .= "From: $from\n";

		return mail($to, $subj, $text, $headers);
	}

	public static function sendWithFiles($from, $to, $subj, $text, array $arrFiles = array()) {

	    if (!Env::isMailFunctionsEnabled()) return;

		$arrParts = array();
		$encoding = Settings::config('system')->get('site', 'encoding');

        //$host = ;;
		//$oSocketConnect = new SocketConnect($host, )
        $boundary = "=_" . md5(uniqid(time()));

        $headers  = '';
        $headers .= "From: $from\n";
	    $headers .= "MIME-Version: 1.0\n";
	    $headers .= 'Content-Type: multipart/mixed; boundary="' . $boundary . "\"\n";

	    $body  = '';
	    $body .= "This is a MIME encoded message.\n\n";
	    $body .= '--' . $boundary . "\n";
	    $body .= 'Content-Type: text/html; charset=' . $encoding . "\n";
	    $body .= 'Content-Transfer-Encoding: Quot-Printed' . "\n\n";
	    $body .= $text ."\n\n";

        foreach ($arrFiles as $filename) {

            $content = File::load($filename);
            //Debug::show('$content ',$content );
            $name = basename($filename);

            $body .= '--' . $boundary . "\n";
            $body .= 'Content-Type: application/octet-stream; name = "' . $name . "\n";
            $body .= 'Content-Transfer-Encoding: base64' . "\n";
            $body .= 'Content-Disposition: attachment; filename = "' . $name . "\n\n";

            $body .= chunk_split(base64_encode($content)) . "\n";
        }

        $body = $body . '--' . $boundary . '--' . "\n";

        mail($to, $subj, $body, $headers);
	}

}
/*
class html_mime_mail {
  var $headers;
  var $multipart;
  var $mime;
  var $html;
  var $parts = array();

//function html_mime_mail($headers="") {
//    $this->headers=$headers;
//}
//
//function add_html($html="") {
//    $this->html.=$html;
//}
//
function build_html($orig_boundary,$kod) {
    $this->multipart.="--$orig_boundary\n";
    if ($kod=='w' || $kod=='win' || $kod=='windows-1251') $kod='windows-1251';
    else $kod='koi8-r';
    $this->multipart.="Content-Type: text/html; charset=$kod\n";
    $this->multipart.="BCC: del@ipo.spb.ru\n";
    $this->multipart.="Content-Transfer-Encoding: Quot-Printed\n\n";
    $this->multipart.="$this->html\n\n";
}


//function add_attachment($path="", $name = "", $c_type="") {
//    if (!file_exists($path.$name)) {
//      print "File $path.$name dosn't exist.";
//      return;
//    }
//    $fp=fopen($path.$name,"r");
//    if (!$fp) {
//      print "File $path.$name coudn't be read.";
//      return;
//    }
//    $file=fread($fp, filesize($path.$name));
//    fclose($fp);
//    $this->parts[]=array("body"=>$file, "name"=>$name,"c_type"=>$c_type);
//}


function build_part($i) {
    $message_part="";
    $message_part.="Content-Type: ".$this->parts[$i]["c_type"];
    if ($this->parts[$i]["name"]!="")
       $message_part.="; name = \"".$this->parts[$i]["name"]."\"\n";
    else
       $message_part.="\n";
    $message_part.="Content-Transfer-Encoding: base64\n";
    $message_part.="Content-Disposition: attachment; filename = \"".
        $this->parts[$i]["name"]."\"\n\n";
    $message_part.=chunk_split(base64_encode($this->parts[$i]["body"]))."\n";
    return $message_part;
}


function build_message($kod) {

	$boundary="=_".md5(uniqid(time()));

    $this->headers.="MIME-Version: 1.0\n";
    $this->headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";

    $this->multipart="";
    $this->multipart.="This is a MIME encoded message.\n\n";

    $this->build_html($boundary,$kod);
    for ($i=(count($this->parts)-1); $i>=0; $i--)
      $this->multipart.="--$boundary\n".$this->build_part($i);

    $this->mime = "$this->multipart--$boundary--\n";
}


function send($server, $to, $from, $subject="", $headers="") {

    $headers="To: $to\nFrom: $from\nSubject: $subject\nX-Mailer: The Mouse!\n$headers";
    $fp = fsockopen($server, 25, &$errno, &$errstr, 30);
    if (!$fp)
       die("Server $server. Connection failed: $errno, $errstr");
    fputs($fp,"HELO $server\n");
    fputs($fp,"MAIL FROM: $from\n");
    fputs($fp,"RCPT TO: $to\n");
    fputs($fp,"DATA\n");
    fputs($fp,$this->headers);
    if (strlen($headers))
      fputs($fp,"$headers\n");
    fputs($fp,$this->mime);
    fputs($fp,"\n.\nQUIT\n");
    while(!feof($fp))
      $resp.=fgets($fp,1024);
    fclose($fp);
  }
}


// *************************************************************************
//
//   В качестве аттача пpисоединяем html-письмо (открывается автоматически).
//   Второй аттач - некоторый файл из каталога.
//   Вот так вызывать все то, что написано выше:
//
// *************************************************************************


  $mail=new html_mime_mail();
  $mail->add_html("<html><body><center><h2>Пpивет!<br><br>".
                  "<br>Посылаю двоичный файл [/bin/ls] ...".
                  "</h2></center></body></html>");
  $mail->add_attachment("/bin/","ls");
  $mail->build_message('win'); // если не "win", то кодиpовка koi8
  $mail->send('ПОЧТОВЫЙ_ХОСТ_ВАШЕГО_ПРОВАЙДЕРА',
              'КОМУ_(E-MAIL)',
              'ОТ_КОГО_(E-MAIL)',
              'ТЕМА ПИСЬМА');

//
// После прихода письма качаем по ФТП оригинальный /bin/ls и сравниваем с
// импортированным из письма:
//
//    X:\temp>fc /b ls ls2
//    Сравнение файлов ls и LS2
//    FC: различия не найдены
//
//
// Внимание! Если у вас нет файла /bin/ls, то просто закомментируйте строку
// $mail->add_attachment("/bin/","ls"), чтобы программа не пыталась присоединить
// к письму неcуществующие файлы.
//
/*
 Date: Mon, 19 Apr 2004 10:30:02 +0300
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - сервер хостера
X-AntiAbuse: Original Domain - имя сайта отправителя
X-AntiAbuse: Originator/Caller UID/GID - [от кого отправлено имя(у хостера)] / [юзер группа]
X-AntiAbuse: Sender Address Domain - домен отправителя
*/
?>